package de.revdev.llt.bean.facade;

import de.revdev.llt.bean.entities.User;
import javax.ejb.EJBException;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;

/**
 *
 * @author f.lindermaier
 */
@Stateless
@LocalBean
public class UserFacade extends AbstractFacade<User> {

    @PersistenceContext(unitName = "LetsLearnTogetherPU")
    private EntityManager em;

    @Override
    public EntityManager getEntityManager() {
        return em;
    }

    public UserFacade() {
        super(User.class);
    }

    public User findByEmail(String email) throws EJBException{
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<User> cq = cb.createQuery(User.class);
        Root<User> r = cq.from(User.class);
        cq.select(r);
        ParameterExpression<String> p = cb.parameter(String.class);
        cq.where(cb.equal(r.get("email"), p));
        return (User) em.createQuery(cq).setParameter(p, email).getSingleResult();
    }
}
